package aom.ju.ss.view.user

import android.view.View
import aom.ju.ss.R
import aom.ju.ss.databinding.UserFragmentBinding
import aom.ju.ss.lpInterface.TitleDialogInterface
import aom.ju.ss.mode.AuditInfo
import aom.ju.ss.mode.UserInfo
import aom.ju.ss.utils.http.LP_API.AUDIT_INFO
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.utils.start
import aom.ju.ss.view.base.BaseFragment
import aom.ju.ss.view.dialog.TitleDialog
import aom.ju.ss.view.user.set.SetActivity
import aom.ju.ss.view.user.shop.ApplyForAShop2Activity
import aom.ju.ss.view.user.shop.ApplyForAShopActivity
import aom.ju.ss.view.user.shop.ShopCheckActivity
import aom.ju.ss.view.user.shop.management.ShopMangerActivity
import aom.ju.ss.view.user.userOrder.UserOrderListActivity
import com.google.gson.Gson


/**
 * 首页 我的
 */
class UserFragment : BaseFragment() {

    companion object {
        fun getMys(): UserFragment {
            return UserFragment()
        }
    }

    lateinit var binding: UserFragmentBinding

    var ck = false


    override fun init() {
        binding = viewBinding as UserFragmentBinding
        binding.user = UserInfo.userInfo

        getAUDITINFO()


        binding.yihuo.setOnClickListener { }
        binding.kanjia.setOnClickListener { }
        binding.diyong.setOnClickListener { }
        binding.jubi.setOnClickListener { }
        binding.shengqindianpu.setOnClickListener {
            ck = true
            getAUDITINFO()
        }


        binding.shengqing.setOnClickListener { binding.shengqindianpu.performClick() }
        binding.dingdan.setOnClickListener { start(UserOrderListActivity::class.java) }
        binding.qiangquan.setOnClickListener { start(CouponGrabRecordLogActivity::class.java)}
        binding.guanzhu.setOnClickListener { }
        binding.shoucang.setOnClickListener { }
        binding.userActivity.setOnClickListener {//个人资料
            start(UserActivity::class.java)
        }
        binding.set.setOnClickListener {//设置
            start(SetActivity::class.java)
        }
        binding.chuangke.setOnClickListener {//成为创客
            start(BecomeAMaker::class.java)
        }


    }

    private fun getAUDITINFO() {// 获取审核结果
        OKHttpUtils(activity, false)
            .SetApiUrl(AUDIT_INFO)
            .POST(this)

    }

    override fun ok(jsonString: String?, httpTY: String?) {
        super.ok(jsonString, httpTY)
        if (httpTY == AUDIT_INFO) {
            goShop(jsonString)
        }
    }

    override fun error(e: String?, jsonString: String?, httpTY: String?) {
        super.error(e, jsonString, httpTY)
        if (httpTY == AUDIT_INFO) {
            try {
                goShop(jsonString)
            } catch (ex: Exception) {

            }
        }
    }

    private fun goShop(jsonString: String?) {
        val mode = Gson().fromJson(jsonString, AuditInfo::class.java)
        binding.adata = mode.data
        if (!ck) {
            return
        }
        if (mode.data == null) {
            start(ApplyForAShopActivity::class.java)
            return
        }

        if (mode.data.audit_status == "0") {//	"audit_status": "审核状态  0待审核 1审核通过  2审核失败",
            start(ShopCheckActivity::class.java)
        } else if (mode.data.audit_status == "1") {//进入店铺
            start(ShopMangerActivity::class.java)
        } else if (mode.data.audit_status == "2") {
            TitleDialog().show(activity, object : TitleDialogInterface {
                override fun ok(view: View?, IsDetermine: Boolean) {
                    if (IsDetermine) {
                        start(ApplyForAShop2Activity::class.java)
                    }
                }

            }).setConnect(mode.data.audit_status_text + "\n原因:" + mode.data.audit_remark)
                .tvDetermine("重新提交")
                .tvCancel("取消")
        }
        ck = false

    }

    override fun getLay(): Int {
        return R.layout.user_fragment
    }
}


